//
//  ViewController.m
//  Wmj_ShoppingCart
//
//  Created by wumengjuan on 16/10/21.
//  Copyright © 2016年 wumengjuan. All rights reserved.
//

#import "ViewController.h"
#import "GoodInfoModel.h"
#import "MycustomTableViewCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,MyCustomCellDelegate>

@property (nonatomic , strong)UITableView *MyTableView;
@property (nonatomic , strong) NSMutableArray * infoArr;
@property (nonatomic , assign) float allPrice;



@property (nonatomic , strong) UIButton * allSelectBtn;
@property (nonatomic , strong) UILabel * allPriceLab;
@end

@implementation ViewController

#pragma mark - 懒加载
-(UITableView *)MyTableView{
    
    if (_MyTableView == nil) {
        _MyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
        _MyTableView .delegate = self;
        _MyTableView .dataSource = self;
        _MyTableView.tableFooterView = [self createFooterView];
    }
    return _MyTableView;
}
-(UIView *)createFooterView{

    UIView * footView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    
    //添加全选文本框标签
    UILabel * lab =[[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width -150, 10, 50, 30)];
    lab.text = @"全选";
    
    [footView addSubview:lab];
    
    
    //添加全选图片按钮
    _allSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _allSelectBtn .frame = CGRectMake(self.view.frame.size.width -100, 10, 30, 30);
    
    [_allSelectBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
    
    [_allSelectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
   
    [footView addSubview:_allSelectBtn];
    
    //添加小结文本框
    
    UILabel *lab2 = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 150, 40, 60, 30)];
    
    lab2.textColor = [UIColor redColor];
    
    lab2.text = @"小结：";
    
    [footView addSubview:lab2];
    
    //添加一个总价文本，用于显示总价
    _allPriceLab = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width -100, 40, 100, 30)];
    
    _allPriceLab.textColor = [UIColor redColor];
    
    _allPriceLab.text = @"0.0";
    
    [footView addSubview:_allPriceLab];
    
    //添加一个结算按钮
    UIButton * settlementBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [settlementBtn setTitle:@"去结算" forState:UIControlStateNormal];
    
    [settlementBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    settlementBtn.frame = CGRectMake(10, 80, self.view.frame.size.width -20, 30);
    
    settlementBtn.backgroundColor = [UIColor blueColor];
    
    [footView addSubview:settlementBtn];
    
    return footView;
}

#pragma mark - 懒加载

-(NSMutableArray *)infoArr{
    if (_infoArr == nil) {
        
        _infoArr =[[NSMutableArray alloc]init];
        
        for (int i = 0; i < 7; i++) {
           
            NSMutableDictionary * infoDic =[[NSMutableDictionary alloc]init];
           
            [infoDic setValue:@"image" forKey:@"imageName"];
            
            [infoDic setValue:@"2000" forKey:@"goodsPrice"];
           
            [infoDic setValue:@"这是商品标题" forKey:@"goodsTitle"];
           
            [infoDic setValue:[NSNumber numberWithBool:NO] forKey:@"selectState"];
           
            [infoDic setValue:[NSNumber numberWithInt:1] forKey:@"goodsNum"];
            
            GoodInfoModel * goodModel  =[[GoodInfoModel alloc]initWithDict:infoDic];
            
            [_infoArr addObject:goodModel];
        }
    }
    return _infoArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"购物车";
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:23.0f]}];

    [self.view addSubview: self.MyTableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.infoArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * identify = @"cell";
    MycustomTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:identify];
    
    if (cell == nil) {
        cell =[[ MycustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.delegate = self;
    }
    [cell addTheValue:self.infoArr[indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 120;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //状态取反
    GoodInfoModel * model =self.infoArr[indexPath.row];
    if (model.selectState) {
        model.selectState = NO;
    }else{
        model.selectState = YES;
    }
    //刷新表格
    [_MyTableView reloadData];
    
    
    [_MyTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [self totalPrice];
}
-(void)btnClick:(UITableViewCell *)cell andFlag:(int)flag{

    NSIndexPath * index =[_MyTableView indexPathForCell:cell];
    switch (flag) {
        case 11 :{
            GoodInfoModel * model =self.infoArr[index.row];
            if (model.goodsNum > 1) {
                model.goodsNum --;
            }
        }
            break;
        case 12:{
            GoodInfoModel * model =self.infoArr[index.row];
                model.goodsNum ++;
        }
            break;
        default:
            break;
    }
    [_MyTableView reloadData];
    
    [self totalPrice];

}
-(void)selectBtnClick:(UIButton *)sender

{
    
    //判断是否选中，是改成否，否改成是，改变图片状态
    
    sender.tag = !sender.tag;
    
    if (sender.tag)
        
    {
        
        [sender setImage:[UIImage imageNamed:@"选中.png"]   forState:UIControlStateNormal];
        
    }else{
        
        [sender setImage:[UIImage imageNamed:@"未选中.png"] forState:UIControlStateNormal];
        
    }
    
    //改变单元格选中状态
    
    for (int i=0; i<self.infoArr.count; i++)
        
    {
        
        GoodInfoModel *model = [self.infoArr objectAtIndex:i];
        
        model.selectState = sender.tag;
        
    }
    
    //计算价格
    
    [self totalPrice];
    
    //刷新表格
    
    [_MyTableView reloadData];
    
}

//2.9 计算总价格

#pragma mark -- 计算价格
-(void)totalPrice
{
    //遍历整个数据源，然后判断如果是选中的商品，就计算价格（单价 * 商品数量）
    
    for ( int i =0; i<self.infoArr.count; i++){
        
        GoodInfoModel *model = [self.infoArr objectAtIndex:i];
        
        if (model.selectState){
            _allPrice = _allPrice + model.goodsNum *[model.goodsPrice intValue];
            
        }
    }
    
    //给总价文本赋值
    
    _allPriceLab.text = [NSString stringWithFormat:@"%.2f",_allPrice];
    
    NSLog(@"%f",_allPrice);
    
    //每次算完要重置为0，因为每次的都是全部循环算一遍
    
    _allPrice = 0.0;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
